package com.wbl.train.business.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.ConfirmOrderService;
import com.wbl.train.common.business.dto.ConfirmOrderDto;
import com.wbl.train.common.business.entity.ConfirmOrderEntity;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王博纶
 * @since 2024-03-04
 * @description 确认订单
 */
@Slf4j
@RestController
@RequestMapping("/admin/confirm-order")
@Tag(name = "确认订单控制器类",
        description = "ConfirmOrderController 确认订单 后端数据接口")
public class ConfirmOrderAdminController {
    @Autowired
    private ConfirmOrderService confirmOrderService;

    /**
     * 通过id查询 确认订单
     * @param confirmOrderDto 确认订单Dto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<ConfirmOrderEntity>> getById(ConfirmOrderDto confirmOrderDto) {
        // 火车每日车次查询
        log.debug("confirmOrderDto :{}",confirmOrderDto);
        PageVo<ConfirmOrderEntity> pageVo = confirmOrderService.queryConfirmOrder(confirmOrderDto);
//        for (ConfirmOrderEntity confirmOrderEntity : pageVo.getDataList()) {
//            confirmOrderEntity.setTickets(null);
//        }
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 确认订单
     * @param confirmOrder 确认订单
     * @return Result
     */
    @Operation(summary = "新增确认订单", description = "新增确认订单")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody ConfirmOrderEntity confirmOrder) {
        return R.ok("保存成功",confirmOrderService.insertAndUpdateConfirmOrder(confirmOrder));
    }

    /**
     * 通过id删除 确认订单
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除确认订单",
               description = "通过id删除确认订单")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(confirmOrderService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前订单信息不存在");
        }
        return R.ok(confirmOrderService.removeById(id));
    }

}
